package com.bootdo.clouddo.utils.spring;

import com.bootdo.clouddo.utils.MD5;
import com.bootdo.clouddo.utils.StringUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.Assert;


public class MD5SaltPasswordEncoder implements PasswordEncoder {

	@Override
	public String encode(CharSequence rawPassword) {
		return null;
	}
	
	public String encode(CharSequence rawPassword, String salt) {
		return MD5.hex_md5(MD5.hex_md5(rawPassword.toString()) + salt);
	}

	@Override
	public boolean matches(CharSequence rawPassword, String encodedPassword) {
		return false;
	}
	
	public boolean matches(String rawPassword, String encodedPassword, String salt) {
		Assert.notNull(salt, "Salt value must be not null");
		if(StringUtils.isBlank(encodedPassword) || StringUtils.isBlank(rawPassword) || StringUtils.isBlank(salt))
			return false;
		return StringUtils.equals(MD5.hex_md5(MD5.hex_md5(rawPassword) + salt), encodedPassword);
	}

}
